﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
	public class campaignUnsubscribesOutput : Api_BaseOutput
	{
		public campaignUnsubscribesResults result { get; set; }
		public campaignUnsubscribesParms inputParms { get; private set; }
		// --------------------------------------------------------
		public campaignUnsubscribesOutput( campaignUnsubscribesInput input )
			: base( input )
		{
			result = new campaignUnsubscribesResults();
			inputParms = input.parms;
		}
		// --------------------------------------------------- tostring
		public override string ToString()
		{
			return string.Format( "Unsubscribe count: {0}", this.result.total );
		}

		#region ================================================================== format setup

		// ---- serial
		internal void Format( string api_Request, string api_Response )
		{
			this.api_Request = api_Request;
			this.api_Response = api_Response;
			// ----------------------------------
			if ( this.api_OutputType == EnumValues.OutputType.JSON )
				outputJSON();
			else if ( this.api_OutputType == EnumValues.OutputType.XML )
				outputXML();
		}

		// ---- xmlrpc
		internal void Format( x_campaignUnsubscribes rpcResult )
		{
			this.api_Request = EnumValues.NotSupplied;
			this.api_Response = EnumValues.NotSupplied;
			// ------------------------
			this.result = FormatResults( rpcResult );
		}

		#endregion

		#region ================================================================= Output

		// ---------------------------------------------------------- outputXML
		private void outputXML()
		{
			XElement xml = XElement.Parse( this.api_Response );
			// error check
			Api_Error error = apiSerial.GetXmlOutputError( xml );
			if ( error != null )
			{
				this.api_ErrorMessages.Add( error );
				return;
			}
			// -------------------------------------------- get values
			try
			{
				double dbl;
				campaignUnsubscribesResults results = new campaignUnsubscribesResults();
				//
				double.TryParse( xml.Element( "total" ).Value, out dbl );
				results.total = dbl;
				// ---------------------------------------- no unsubscribe form info
				if ( xml.Element( "data" ).HasElements )
				{
					IEnumerable<XElement> data = xml.Element( "data" ).Elements( "struct" );
					foreach ( XElement entry in data )
					{
						campaignUnsubscribesResults.Unsubscribes unsub = new campaignUnsubscribesResults.Unsubscribes();
						if ( entry.HasElements )
						{
							// complete structure ???
							unsub.email = entry.Element( "email" ).Value ?? "";
							unsub.reason = entry.Element( "reason" ).Value ?? "";
							unsub.reason_text = entry.Element( "reason_text" ).Value ?? "";
							//
							results.data.Add( unsub );
						}
					}
				}
				//
				this.result = results;
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}

		// ---------------------------------------------------------- outputJSON
		private void outputJSON()
		{
			JavaScriptSerializer jss = new JavaScriptSerializer();
			try
			{
				if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
				{
					Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
					this.api_ErrorMessages.Add( err );
				}
				else
				{
					x_campaignUnsubscribes jsonResult = jss.Deserialize<x_campaignUnsubscribes>( this.api_Response );
					this.result = FormatResults( jsonResult );
				}
			}
			catch ( Exception ex )
			{
				this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
			}
		}
		// ---------------------------------------------------------- FormatResults
		private campaignUnsubscribesResults FormatResults( x_campaignUnsubscribes x_results )
		{
			campaignUnsubscribesResults results = new campaignUnsubscribesResults();
			results.total = x_results.total;
			//
			foreach ( var item in x_results.data )
			{
				campaignUnsubscribesResults.Unsubscribes unsub = new campaignUnsubscribesResults.Unsubscribes();
				IDictionary dict = item as IDictionary;
				unsub.email = dict["email"].ToString();
				unsub.reason = ( dict["reason"] == null ) ? "" : dict["reason"].ToString();
				unsub.reason_text = ( dict["reason_text"] == null ) ? "" : dict["reason_text"].ToString();
				//
				results.data.Add( unsub );
			}
			return results;
		}
		// ---------

		#endregion
	}
}
